package com.ticket.sass.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.TicketServerConfigMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.TicketServerConfigEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-07
 */
@Service
public class TicketServerConfigServiceImpl extends ServiceImpl<TicketServerConfigMapper, TicketServerConfigEntity> implements BaseService<TicketServerConfigEntity> {
    public TicketServerConfigEntity findByCinemaId(Long cinemaId){
        return this.getOne(new QueryWrapper<TicketServerConfigEntity>().eq("cinema_id", cinemaId).last("limit 1"));
    }
    
    public boolean updateByCinemaId(Long cinemaId, TicketServerConfigEntity ticketServerConfigEntity) {
        UpdateWrapper<TicketServerConfigEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cinema_id", cinemaId);
        return this.baseMapper.update(ticketServerConfigEntity, updateWrapper) > 0;
    }
    
    /**
     * 根据影院ID列表查询对应的票务服务器配置信息，并返回一个Map。
     * @param cinemaIds 影院ID列表
     * @return 包含票务服务器配置信息的Map，键为实体的ID，值为对应的实体对象
     */
    public Map<Long, TicketServerConfigEntity> getTicketServerConfigMapByCinemaIds(List<Long> cinemaIds) {
        if (cinemaIds.size() == 0){
            return new HashMap<>();
        }
        
        Map<Long, TicketServerConfigEntity> resultMap = new HashMap<>();
        // 根据影院ID列表查询对应的票务服务器配置信息
        List<TicketServerConfigEntity> ticketServerConfigList = list(new QueryWrapper<TicketServerConfigEntity>().in("cinema_id", cinemaIds));
        // 将查询结果转换为Map
        for (TicketServerConfigEntity ticketServerConfigEntity : ticketServerConfigList) {
            resultMap.put(ticketServerConfigEntity.getCinemaId(), ticketServerConfigEntity);
        }
        return resultMap;
    }
}
